﻿using K9Nano.Reporting;
using K9Nano.Serialization;
using Microsoft.EntityFrameworkCore;

namespace K9Nano.Domain.Reporting;

public class ReportDbContext: DbContext
{
    public ReportDbContext(DbContextOptions<ReportDbContext> options) : base(options)
    {
    }

    public DbSet<ReportEntity> Reports { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ReportEntity>(et =>
        {
            et.ToTable("report");

            et.HasKey(a => a.Id);
            et.Property(a => a.Id).ValueGeneratedOnAdd();

            et.Property(a => a.Fields)
              .HasConversion(
                  v => JsonHelper.Serialize(v),
                  v => JsonHelper.Deserialize<List<ReportFormItem>>(v)!);

            et.Property(a => a.Columns)
              .HasConversion(
                  v => JsonHelper.Serialize(v),
                  v => JsonHelper.Deserialize<List<ReportColumn>>(v)!);
        });
    }
}
